package laba3;

import java.util.Vector;
import java.util.concurrent.Callable;

/**
 * Created by Mikhail on 07.05.14.
 * Merge two vectors into the sorted one.
 */
public class Sorter implements Callable<Vector<Integer>> {
    Vector<Integer> left,right,arr;

    Sorter() {
        left = new Vector<Integer>();
        right = new Vector<Integer>();
        arr = new Vector<Integer>();
    }
    Sorter(int el) {
        this();
        left.add(el);
    }
    Sorter(Vector<Integer> arr) {
        this();
        left = arr;
    }
    Sorter(Vector<Integer> left, Vector<Integer> right) {
        this();
        this.left = left;
        this.right = right;
    }

    @Override
    public Vector<Integer> call() throws Exception {
        int leftI = 0, rightI = 0;
        while (!((leftI >= left.size())||(rightI >= right.size()))) {
            if (left.elementAt(leftI) < right.elementAt(rightI)) {
                arr.add(left.elementAt(leftI));
                leftI++;
            } else {
                arr.add(right.elementAt(rightI));
                rightI++;
            }
        }
        for (; leftI < left.size(); leftI++) arr.add(left.elementAt(leftI));
        for (; rightI < right.size(); rightI++) arr.add(right.elementAt(rightI));
        return arr;
    }
}
